package com.jackstraw.jack_base.datasource.moredatasource.bean;


import com.alibaba.druid.pool.DruidDataSource;
import com.jackstraw.jack_base.datasource.database.parten.entity.bean.DataBaseFactory;
import org.apache.commons.lang3.ObjectUtils;

import javax.sql.DataSource;
import java.sql.SQLException;


public class DruidDataBaseFactory extends DataBaseFactory<DruidDataBaseEntity> {


    @Override
    protected DataSource dataSource(DruidDataBaseEntity druidDataBaseEntity) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setKeepAlive(druidDataBaseEntity.getKeepAlive());
        druidDataSource.setPoolPreparedStatements(druidDataBaseEntity.getPoolPreparedStatements());
        druidDataSource.setMaxActive(druidDataBaseEntity.getMaxActive());
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(druidDataBaseEntity.getMaxPoolPreparedStatementPerConnectionSize());
        druidDataSource.setConnectionInitSqls(druidDataBaseEntity.getConnectionInitSqls());
        druidDataSource.setMinEvictableIdleTimeMillis(druidDataBaseEntity.getMinEvictableIdleTimeMillis());
        druidDataSource.setMaxEvictableIdleTimeMillis(druidDataBaseEntity.getMaxEvictableIdleTimeMillis());
        druidDataSource.setTimeBetweenEvictionRunsMillis(druidDataBaseEntity.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setValidationQuery(druidDataBaseEntity.getValidationQuery());
        druidDataSource.setValidationQueryTimeout(druidDataBaseEntity.getValidationQueryTimeout());
        druidDataSource.setTestOnBorrow(druidDataBaseEntity.getTestOnBorrow());
        druidDataSource.setTestOnReturn(druidDataBaseEntity.getTestOnReturn());
        druidDataSource.setTestWhileIdle(druidDataBaseEntity.getTestWhileIdle());
        druidDataSource.setName(druidDataBaseEntity.getName());
        druidDataSource.setPoolPreparedStatements(druidDataBaseEntity.getPoolPreparedStatements());
        druidDataSource.setMaxWait(druidDataBaseEntity.getMaxWait());
        druidDataSource.setMinIdle(druidDataBaseEntity.getMinIdle());
        druidDataSource.setInitialSize(druidDataBaseEntity.getInitialSize());
        druidDataSource.setMaxActive(druidDataBaseEntity.getMaxActive());
        druidDataSource.setUsername(druidDataBaseEntity.getUsername());
        druidDataSource.setPassword(druidDataBaseEntity.getPassword());
        druidDataSource.setDriverClassName(druidDataBaseEntity.getDriverClass());
        druidDataSource.setUrl(druidDataBaseEntity.getJdbcUrl());
        if (ObjectUtils.isNotEmpty(druidDataBaseEntity.getProxyFilters())
                && druidDataBaseEntity.getProxyFilters().size() > 0) {
            druidDataSource.setProxyFilters(druidDataBaseEntity.getProxyFilters());
        }
        druidDataSource.setName(druidDataBaseEntity.getName());

        try {
            druidDataSource.init();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return druidDataSource;
    }
}
